package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.entity.SpuInfoEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;

import com.atguigu.gulimall.product.dao.SkuInfoDao;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
        new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> qw = new QueryWrapper<>();
        String key = params.get("key").toString();
        if (StringUtils.isNotEmpty(key)){
            qw.and(s->{s.lambda()
                    .eq(SkuInfoEntity::getSkuId,key)
                    .or().like(SkuInfoEntity::getSkuName,key);});
        }

        String brandId = params.get("brandId").toString();
        if (StringUtils.isNotEmpty(brandId)&&!brandId.equals("0")){

            qw.lambda().eq(SkuInfoEntity::getBrandId,brandId);
        }
        String catelogId = params.get("catelogId").toString();
        if (StringUtils.isNotEmpty(catelogId)&&!catelogId.equals("0")){
            qw.lambda().eq(SkuInfoEntity::getCatalogId,catelogId);

        }

        String min = params.get("min").toString();
        if (StringUtils.isNotEmpty(min)&&!min.equals("0")){
            qw.lambda().ge(SkuInfoEntity::getPrice,min);
        }
        String max = params.get("max").toString();
        if (StringUtils.isNotEmpty(max)&&!max.equals("0")){
            qw.lambda().le(SkuInfoEntity::getPrice,max);
        }


        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),qw);

        return new PageUtils(page);
    }

}
